package fr.fermeManager.application.views.components;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;

import fr.fermeManager.library.model.beans.Ferme;
import javax.swing.border.TitledBorder;
import java.awt.Dimension;

public class ComponentFerme extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private BindingGroup m_bindingGroup;
	private fr.fermeManager.library.model.beans.Ferme ferme = new fr.fermeManager.library.model.beans.Ferme();
	private JCheckBox actifJCheckBox;
	private JTextField libelleJTextField;
	private JTextField descriptionJTextField;
	private JCheckBox principaleJCheckBox;
	private JLabel lblLibellevalueid;

	public ComponentFerme(Ferme newFerme,boolean existeActifs) {
		this(existeActifs);
		setFerme(newFerme);
	}

	/**
	 * @wbp.parser.constructor
	 */
	public ComponentFerme(boolean existeActifs) {
		setMinimumSize(new Dimension(450, 200));
		setPreferredSize(new Dimension(450, 200));
		setBorder(new TitledBorder(null, "Edition Ferme", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setLayout(new MigLayout("", "[57px,grow][377px,grow]", "[21px,grow][14px,grow][20px,grow][20px,grow][21px,grow]"));

		JLabel actifLabel = new JLabel("Actif:");
		add(actifLabel, "cell 0 0,alignx center,aligny center");

		actifJCheckBox = new JCheckBox();
		actifJCheckBox.setEnabled(!existeActifs);
		add(actifJCheckBox, "flowx,cell 1 0,growx,aligny center");

		JLabel idLabel = new JLabel("Id:");
		add(idLabel, "cell 0 1,alignx center,aligny center");
		
		lblLibellevalueid = new JLabel("libelleValueId");
		add(lblLibellevalueid, "cell 1 1");

		JLabel libelleLabel = new JLabel("Libelle:");
		add(libelleLabel, "cell 0 2,alignx center,aligny center");

		libelleJTextField = new JTextField();
		add(libelleJTextField, "cell 1 2,growx,aligny center");

		JLabel descriptionLabel = new JLabel("Description:");
		add(descriptionLabel, "cell 0 3,alignx center,aligny center");

		descriptionJTextField = new JTextField();
		add(descriptionJTextField, "cell 1 3,growx,aligny center");

		JLabel principaleLabel = new JLabel("Principale:");
		add(principaleLabel, "cell 0 4,alignx center,aligny center");

		principaleJCheckBox = new JCheckBox();
		add(principaleJCheckBox, "cell 1 4,growx,aligny center");
		
		JLabel libelleActifs = new JLabel("( Poss\u00E8de des \u00E9l\u00E9ments Actifs )");
		libelleActifs.setVisible(existeActifs);
		add(libelleActifs, "cell 1 0");

		if (ferme != null) {
			m_bindingGroup = initDataBindings();
		}
	}

	public fr.fermeManager.library.model.beans.Ferme getFerme() {
		return ferme;
	}

	public void setFerme(fr.fermeManager.library.model.beans.Ferme newFerme) {
		setFerme(newFerme, true);
	}

	public void setFerme(fr.fermeManager.library.model.beans.Ferme newFerme, boolean update) {
		ferme = newFerme;
		if (update) {
			if (m_bindingGroup != null) {
				m_bindingGroup.unbind();
				m_bindingGroup = null;
			}
			if (ferme != null) {
				m_bindingGroup = initDataBindings();
			}
		}
	}
	protected BindingGroup initDataBindings() {
		BeanProperty<Ferme, Boolean> actifProperty = BeanProperty.create("actif");
		BeanProperty<JCheckBox, Boolean> selectedProperty = BeanProperty.create("selected");
		AutoBinding<Ferme, Boolean, JCheckBox, Boolean> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, ferme, actifProperty, actifJCheckBox, selectedProperty);
		autoBinding.bind();
		//
		BeanProperty<Ferme, String> libelleProperty = BeanProperty.create("libelle");
		BeanProperty<JTextField, String> textProperty_1 = BeanProperty.create("text");
		AutoBinding<Ferme, String, JTextField, String> autoBinding_2 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, ferme, libelleProperty, libelleJTextField, textProperty_1);
		autoBinding_2.bind();
		//
		BeanProperty<Ferme, String> descriptionProperty = BeanProperty.create("description");
		BeanProperty<JTextField, String> textProperty_2 = BeanProperty.create("text");
		AutoBinding<Ferme, String, JTextField, String> autoBinding_3 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, ferme, descriptionProperty, descriptionJTextField, textProperty_2);
		autoBinding_3.bind();
		//
		BeanProperty<Ferme, Boolean> principaleProperty = BeanProperty.create("principale");
		BeanProperty<JCheckBox, Boolean> selectedProperty_1 = BeanProperty.create("selected");
		AutoBinding<Ferme, Boolean, JCheckBox, Boolean> autoBinding_4 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, ferme, principaleProperty, principaleJCheckBox, selectedProperty_1);
		autoBinding_4.bind();
		//
		BeanProperty<Ferme, Integer> fermeBeanProperty = BeanProperty.create("id");
		BeanProperty<JLabel, String> jLabelBeanProperty = BeanProperty.create("text");
		AutoBinding<Ferme, Integer, JLabel, String> autoBinding_1 = Bindings.createAutoBinding(UpdateStrategy.READ, ferme, fermeBeanProperty, lblLibellevalueid, jLabelBeanProperty);
		autoBinding_1.bind();
		//
		BindingGroup bindingGroup = new BindingGroup();
		//
		bindingGroup.addBinding(autoBinding);
		bindingGroup.addBinding(autoBinding_2);
		bindingGroup.addBinding(autoBinding_3);
		bindingGroup.addBinding(autoBinding_4);
		bindingGroup.addBinding(autoBinding_1);
		return bindingGroup;
	}
}
